/*
Copyright (C) 2007 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// @fileoverview Class for handling menu creation and click events

function Menus() {
  this.menuRefersToClock = -1;
}

/*
 * Called right before the menu is rendered - this is where menu items are added
 */
Menus.prototype.onAddCustomMenuItems = function(menu) {
  tatwGadget.menus.menuRefersToClock = tatwGadget.clockPanel.focusedClock;
  tatwGadget.clockPanel.focusedClock = -1;
  menu.AddItem(MENU_ADD_CLOCK,
               tatwGadget.clockPanel.clocks.length < MAX_CLOCKS ?
               0 : gddMenuItemFlagGrayed, tatwGadget.menus.addClock);
  if (tatwGadget.menus.menuRefersToClock > -1) {
    menu.AddItem(MENU_EDIT + ' \'' + tatwGadget.clockPanel.clocks[
        tatwGadget.menus.menuRefersToClock].cityLabel.innerText + '\'',
        0, tatwGadget.menus.editThisClock);
    menu.AddItem(MENU_REMOVE + ' \'' + tatwGadget.clockPanel.clocks[
        tatwGadget.menus.menuRefersToClock].cityLabel.innerText + '\'',
        (tatwGadget.clockPanel.clocks.length > 1 ? 0 : gddMenuItemFlagGrayed),
        tatwGadget.menus.removeThisClock);
  } else {
    var subMenu_EditClock = menu.AddPopup(MENU_EDIT_CLOCK);
    var i = 0;
    for (; i < tatwGadget.clockPanel.clocks.length; ++i) {
      subMenu_EditClock.AddItem(
          tatwGadget.clockPanel.clocks[i].cityLabel.innerText, 0,
          (function (i) {
            return function() {
              tatwGadget.menus.editClock(i);
            };
          })(i));
    }
    if (tatwGadget.clockPanel.clocks.length > 1) {
      var subMenu_RemoveClock = menu.AddPopup(MENU_REMOVE_CLOCK);
      for (i = 0; i < tatwGadget.clockPanel.clocks.length; ++i) {
        subMenu_RemoveClock.AddItem(
            tatwGadget.clockPanel.clocks[i].cityLabel.innerText, 0,
            (function (i) {
              return function() {
                tatwGadget.menus.removeClock(i);
              };
            })(i));
      }
    } else {
      menu.AddItem(MENU_REMOVE_CLOCK, gddMenuItemFlagGrayed,
                   tatwGadget.menus.removeClock);
    }
  }
};

/*
 * Called when the user clicks on the Add clock menu - it opens the add dialog
 */
Menus.prototype.addClock = function() {
  tatwGadget.menus.menuRefersToClock = -1;
  options.putValue(OPTION_NAMES.OPTIONS_DIALOG_TYPE, 'ADD');
  plugin.ShowOptionsDialog();
};

/*
 * Called when the user clicks on edit a clock and a clock name after
 * &param {Integer} nr is the index of the clock to be edited
 */
Menus.prototype.editClock = function(nr) {
  var clock = tatwGadget.clockPanel.clocks[nr];
  options.putValue(OPTION_NAMES.EDIT_CLOCK_NUM, nr);
  options.putValue(OPTION_NAMES.EDIT_CLOCK_OFFSET, clock.offsetMinutes);
  options.putValue(OPTION_NAMES.EDIT_CLOCK_LOCATION, clock.cityLabel.innerText);
  options.putValue(OPTION_NAMES.DESIGN, clock.designId);
  options.putValue(OPTION_NAMES.BACKGROUND, clock.backgroundId);
  options.putValue(OPTION_NAMES.OPTIONS_DIALOG_TYPE, 'EDIT');
  tatwGadget.onMouseOver();
  plugin.ShowOptionsDialog();
};

/*
 * Called when the user clicks on remove a clock and a clock name after
 * &param {Integer} nr is the index of the clock to be removed
 */
Menus.prototype.removeClock = function(nr) {
  if (nr < 0 || nr >= tatwGadget.clockPanel.clocks.length) {
    return;
  }
  tatwGadget.clockPanel.clocks[nr].destroyClock();
  tatwGadget.clockPanel.clocks =
      tatwGadget.clockPanel.clocks.slice(0, nr).concat(
      tatwGadget.clockPanel.clocks.slice(nr + 1,
      tatwGadget.clockPanel.clocks.length));
  var totalClocks = tatwGadget.clockPanel.clocks.length;
  options.putValue(OPTION_NAMES.TOTAL_CLOCKS,
                   tatwGadget.clockPanel.clocks.length);
  var i;
  for (i = nr; i < totalClocks; ++i) {
    options.putValue(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_OFFSET,
        tatwGadget.clockPanel.clocks[i].offsetMinutes);
    options.putValue(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_DESIGN,
        tatwGadget.clockPanel.clocks[i].designId);
    options.putValue(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_BACKGROUND,
        tatwGadget.clockPanel.clocks[i].backgroundId);
    options.putValue(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_LOCATION,
        tatwGadget.clockPanel.clocks[i].cityLabel.innerText);
    tatwGadget.clockPanel.clocks[i].eventArea.onmouseover =
        'tatwGadget.clockPanel.focusedClock = ' + i + ';';
  }
  for (i = totalClocks; i < MAX_CLOCKS; ++i) {
    options.remove(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_OFFSET);
    options.remove(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_DESIGN);
    options.remove(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_BACKGROUND);
    options.remove(OPTION_NAMES.NUMBERED_PREFIX + i +
        OPTION_NAMES.NUMBERED_LOCATION);
  }

  if ((totalClocks == 8 && tatwGadget.clockPanel.clockArrangement > 3) ||
      (totalClocks == 2 && tatwGadget.clockPanel.clockArrangement > 1)) {
    tatwGadget.clockPanel.clockArrangement -= 2;
    options.putValue(OPTION_NAMES.CLOCK_ARRANGEMENT,
                     tatwGadget.clockPanel.clockArrangement);
  } else {
    tatwGadget.clockPanel.arrangeClocks();
  }
  tatwGadget.onMouseOver();
  tatwGadget.onMouseOut();
};

/*
 * Called when the user clicks the Edit '[location name]' menu; this function
 * just invokes Menus.prototype.editClock for the stored current clock
 */
Menus.prototype.editThisClock = function() {
  if (tatwGadget.menus.menuRefersToClock > -1) {
    tatwGadget.menus.editClock(tatwGadget.menus.menuRefersToClock);
    tatwGadget.menus.menuRefersToClock = -1;
  }
};

/*
 * Called when the user clicks the Remove '[location name]' menu; this function
 * just invokes Menus.prototype.removeClock for the stored current clock
 */
Menus.prototype.removeThisClock = function() {
  if (tatwGadget.menus.menuRefersToClock > -1) {
    tatwGadget.menus.removeClock(tatwGadget.menus.menuRefersToClock);
    tatwGadget.menus.menuRefersToClock = -1;
  }
};
